package com.zzld.core.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.SecurityUtils;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import com.zzld.core.common.config.Global;

/**
 * 
 * @ClassName: SessionInterceptor
 * @Description: TODO session 拦截器
 * @author: zzld
 * @date: 2015年4月7日 下午10:21:51
 */
public class SessionInterceptor extends HandlerInterceptorAdapter {

	@Override
	public boolean preHandle(HttpServletRequest request,HttpServletResponse response, Object handler) throws Exception {
		if (request.getHeader("x-requested-with") != null &&
			request.getHeader("x-requested-with").equalsIgnoreCase("XMLHttpRequest")){
			if (!SecurityUtils.getSubject().isAuthenticated()) {
				String ctx = request.getContextPath();
				// 如果是ajax请求响应头会有，x-requested-with；	
				response.setContentType("text/html;charset=utf-8");
				response.getWriter().print("长时间未操作,链接断开,请刷新页面,重新登录！"); // 打印一个返回值，没这一行，在tabs页中无法跳出
				return false;
			}
		}
		return true;
	}

}
